<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="表名">
              <a-input v-model="queryParam.sheet" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="导入编号">
              <a-input v-model="queryParam.code" placeholder=""/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="24">
              <a-form-item label="日志级别">
                <a-select v-model="queryParam.type">
                  <a-select-option v-for="option in typeOptions" :key="option.key" :value="option.key">
                    {{ option.title }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="16" :sm="24">
              <a-form-item label="日志时间">
                <a-range-picker @change="onChange"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
              <a @click="toggleAdvanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
    >
      <span slot="importLogType" slot-scope="text">
        <a-tooltip placement="topLeft">
          <a-tag :color="text | typeColorFilter">{{ text | typeTextFilter }}</a-tag>
        </a-tooltip>
      </span>
    </s-table>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '@/components'
import { queryImportLog } from '@/api/basic/importLog'

// 用于处理过滤器动态获取数据
let that

const typeMap = {
  1: {color: 'cyan', text: '测试'},
  2: {color: 'green', text: '正常'},
  3: {color: 'yellow', text: '警告'},
  4: {color: 'red', text: '错误'},
  5: {color: '#f50', text: '导入失败'},
}

export default {
  name: 'ImportLogList',
  components: {
    STable,
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      typeOptions: [
        {key: 1, value: 'DEBUG', title: '测试'},
        {key: 2, value: 'INFO', title: '正常'},
        {key: 3, value: 'WARNING', title: '警告'},
        {key: 4, value: 'ERROR', title: '错误'},
        {key: 5, value: 'FETAL', title: '导入失败'},
      ],
      // 表头
      columns: [
        {
          title: '导入表',
          dataIndex: 'importLogSheet',
          align: 'center'
        },
        {
          title: '导入编号',
          dataIndex: 'importLogCode',
          align: 'center'
        },
        {
          title: '日志级别',
          dataIndex: 'importLogType',
          scopedSlots: { customRender: 'importLogType' },
          align: 'center'
        },
        {
          title: '日志内容',
          dataIndex: 'importLogContent',
          align: 'center'
        },
        {
          title: '导入时间',
          dataIndex: 'gmtCreate',
          align: 'center'
        },
        // {
        //   title: '操作',
        //   dataIndex: 'action',
        //   align: 'center',
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        return queryImportLog(Object.assign(parameter, queryJson))
          .then(res => {
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      // custom table alert & rowSelection
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {
    typeTextFilter (type) {
      return typeMap[type].text
    },
    typeColorFilter (type) {
      return typeMap[type].color
    },
  },
  beforeCreate () {
    that = this
  },
  created () {
  },
  methods: {
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    onChange (date, dateString) {
      this.queryParam.begDate = dateString[0] === '' ? undefined : dateString[0]
      this.queryParam.endDate = dateString[1] === '' ? undefined : dateString[1]
    },
  }
}
</script>
